/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.SetUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.EntryType;
/*     */ import java.util.HashSet;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class OperationContextData
/*     */ {
/*     */   protected OperationContext mOctxt;
/*     */   
/*     */   protected OperationContextData(OperationContext octxt)
/*     */   {
/*  34 */     this.mOctxt = octxt;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void addGranteeNames(OperationContext octxt, Mailbox.FolderNode node)
/*     */   {
/*  53 */     if ((octxt == null) || (node == null)) {
/*  54 */       return;
/*     */     }
/*  56 */     GranteeNames data = getOrInitGranteeNames(octxt);
/*  57 */     data.addRootNode(node);
/*     */   }
/*     */   
/*     */   public static GranteeNames getGranteeNames(OperationContext octxt) {
/*  61 */     return octxt == null ? null : (GranteeNames)octxt.getCtxtData("GranteeNames");
/*     */   }
/*     */   
/*     */   public static void setNeedGranteeName(OperationContext octxt, boolean needGranteeName) {
/*  65 */     if (octxt == null) {
/*  66 */       return;
/*     */     }
/*  68 */     GranteeNames data = getOrInitGranteeNames(octxt);
/*  69 */     data.setNeedGranteeName(needGranteeName);
/*     */   }
/*     */   
/*     */   public static boolean getNeedGranteeName(OperationContext octxt) {
/*  73 */     if (octxt == null) {
/*  74 */       return true;
/*     */     }
/*     */     
/*  77 */     GranteeNames data = getGranteeNames(octxt);
/*  78 */     if (data == null) {
/*  79 */       return true;
/*     */     }
/*     */     
/*  82 */     return data.needGranteeName();
/*     */   }
/*     */   
/*     */   private static GranteeNames getOrInitGranteeNames(OperationContext octxt) {
/*  86 */     GranteeNames data = getGranteeNames(octxt);
/*  87 */     if (data == null) {
/*  88 */       data = new GranteeNames(octxt);
/*  89 */       octxt.setCtxtData("GranteeNames", data);
/*     */     }
/*  91 */     return data;
/*     */   }
/*     */   
/*     */ 
/*     */   public static class GranteeNames
/*     */     extends OperationContextData
/*     */   {
/*     */     private static final String KEY = "GranteeNames";
/*     */     
/*     */     private static final int USR_GRANTEES = 0;
/*     */     
/*     */     private static final int GRP_GRANTEES = 1;
/*     */     
/*     */     private static final int COS_GRANTEES = 2;
/*     */     
/*     */     private static final int DOM_GRANTEES = 3;
/*     */     private static final int NUM_GRANTEE_TYPES = 4;
/* 108 */     private boolean mEncounteredLDAPFailure = false;
/* 109 */     private boolean mNeedGranteeName = true;
/*     */     
/*     */     private Set<Mailbox.FolderNode> mUnresolvedRootNodes;
/*     */     
/*     */     private Set<Mailbox.FolderNode> mResolvedRootNodes;
/*     */     
/* 115 */     private Map<String, String>[] mIdsToNamesMap = new Map[4];
/*     */     public static final String EMPTY_NAME = "";
/*     */     
/* 118 */     GranteeNames(OperationContext octxt) { super(); }
/*     */     
/*     */     void setNeedGranteeName(boolean needGranteeName)
/*     */     {
/* 122 */       this.mNeedGranteeName = needGranteeName;
/*     */     }
/*     */     
/*     */     boolean needGranteeName() {
/* 126 */       return this.mNeedGranteeName;
/*     */     }
/*     */     
/*     */     void addRootNode(Mailbox.FolderNode node) {
/* 130 */       if (this.mUnresolvedRootNodes == null) {
/* 131 */         this.mUnresolvedRootNodes = new HashSet();
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 139 */       boolean alreadyResolved = false;
/* 140 */       if (this.mResolvedRootNodes != null) {
/* 141 */         for (Mailbox.FolderNode resolvedNode : this.mResolvedRootNodes) {
/* 142 */           if (resolvedNode.mId == node.mId) {
/* 143 */             alreadyResolved = true;
/* 144 */             break;
/*     */           }
/*     */         }
/*     */       }
/*     */       
/*     */ 
/* 150 */       boolean alreadyAdded = false;
/* 151 */       for (Mailbox.FolderNode unresolvedNode : this.mUnresolvedRootNodes) {
/* 152 */         if (unresolvedNode.mId == node.mId) {
/* 153 */           alreadyAdded = true;
/* 154 */           break;
/*     */         }
/*     */       }
/*     */       
/*     */ 
/* 159 */       if ((!alreadyResolved) && (!alreadyAdded)) {
/* 160 */         this.mUnresolvedRootNodes.add(node);
/*     */       }
/*     */     }
/*     */     
/*     */     private void resolveIfNecessary() {
/* 165 */       if ((this.mUnresolvedRootNodes == null) || (this.mUnresolvedRootNodes.isEmpty())) {
/* 166 */         return;
/*     */       }
/* 168 */       for (Mailbox.FolderNode unresolvedNode : this.mUnresolvedRootNodes)
/*     */       {
/* 170 */         Set[] idHolders = new Set[4];
/* 171 */         collectGranteeIds(unresolvedNode, idHolders);
/*     */         
/*     */ 
/* 174 */         for (int bucket = 0; bucket < 4; bucket++) {
/* 175 */           if ((idHolders[bucket] != null) && (this.mIdsToNamesMap[bucket] != null)) {
/* 176 */             idHolders[bucket] = SetUtil.subtract(idHolders[bucket], this.mIdsToNamesMap[bucket].keySet());
/*     */           }
/*     */         }
/* 179 */         populateIdToNameMaps(idHolders);
/*     */       }
/*     */       
/*     */ 
/* 183 */       if (this.mResolvedRootNodes == null) {
/* 184 */         this.mResolvedRootNodes = new HashSet();
/*     */       }
/* 186 */       this.mResolvedRootNodes.addAll(this.mUnresolvedRootNodes);
/* 187 */       this.mUnresolvedRootNodes.clear();
/*     */     }
/*     */     
/*     */     private void populateIdToNameMaps(Set<String>[] idHolders) {
/* 191 */       Map<String, String> result = null;
/*     */       
/* 193 */       for (int bucket = 0; bucket < 4; bucket++) {
/* 194 */         if ((idHolders[bucket] != null) && (!idHolders[bucket].isEmpty()))
/*     */         {
/*     */           try
/*     */           {
/* 198 */             Provisioning.EntryType entryType = null;
/* 199 */             if (bucket == 0) {
/* 200 */               entryType = Provisioning.EntryType.account;
/* 201 */             } else if (bucket == 1) {
/* 202 */               entryType = Provisioning.EntryType.group;
/* 203 */             } else if (bucket == 2) {
/* 204 */               entryType = Provisioning.EntryType.cos;
/* 205 */             } else if (bucket == 3) {
/* 206 */               entryType = Provisioning.EntryType.domain;
/*     */             }
/* 208 */             if (entryType != null) {
/* 209 */               result = Provisioning.getInstance().getNamesForIds(idHolders[bucket], entryType);
/*     */             }
/*     */           } catch (ServiceException e) {
/* 212 */             ZimbraLog.mailbox.warn("cannot lookup user grantee names", e);
/* 213 */             this.mEncounteredLDAPFailure = true;
/*     */           }
/*     */           
/* 216 */           if (result != null)
/* 217 */             if (this.mIdsToNamesMap[bucket] == null) {
/* 218 */               this.mIdsToNamesMap[bucket] = result;
/*     */             } else
/* 220 */               this.mIdsToNamesMap[bucket].putAll(result);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     private void collectGranteeIds(Mailbox.FolderNode node, Set<String>[] idHolders) {
/* 226 */       if (node.mFolder != null) {
/* 227 */         ACL acl = node.mFolder.getEffectiveACL();
/* 228 */         collectGranteeIdsOnACL(acl, idHolders);
/*     */       }
/*     */       
/* 231 */       for (Mailbox.FolderNode subNode : node.mSubfolders)
/* 232 */         collectGranteeIds(subNode, idHolders);
/*     */     }
/*     */     
/*     */     int getGranteeBucket(byte granteeType) {
/* 236 */       switch (granteeType) {
/*     */       case 1: 
/* 238 */         return 0;
/*     */       case 2: 
/* 240 */         return 1;
/*     */       case 5: 
/* 242 */         return 2;
/*     */       case 4: 
/* 244 */         return 3;
/*     */       }
/* 246 */       return -1;
/*     */     }
/*     */     
/*     */     private void collectGranteeIdsOnACL(ACL acl, Set<String>[] idHolders)
/*     */     {
/* 251 */       if (acl != null) {
/* 252 */         for (ACL.Grant grant : acl.getGrants()) {
/* 253 */           int idx = getGranteeBucket(grant.getGranteeType());
/* 254 */           if (idx != -1) {
/* 255 */             if (idHolders[idx] == null) {
/* 256 */               idHolders[idx] = new HashSet();
/*     */             }
/* 258 */             idHolders[idx].add(grant.getGranteeId());
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 265 */     public static final String INVALID_GRANT = new String("***INVALID***");
/*     */     
/*     */     public String getNameById(String id, byte granteeType) {
/* 268 */       resolveIfNecessary();
/*     */       
/* 270 */       int idx = getGranteeBucket(granteeType);
/* 271 */       if (idx != -1)
/*     */       {
/*     */ 
/*     */ 
/*     */ 
/* 276 */         if (this.mIdsToNamesMap[idx] == null) {
/* 277 */           return null;
/*     */         }
/* 279 */         String name = (String)this.mIdsToNamesMap[idx].get(id);
/*     */         
/*     */ 
/* 282 */         if (name == null)
/*     */         {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 291 */           if (this.mEncounteredLDAPFailure) {
/* 292 */             return "";
/*     */           }
/* 294 */           return INVALID_GRANT;
/*     */         }
/*     */         
/* 297 */         return name;
/*     */       }
/*     */       
/* 300 */       return null;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/OperationContextData.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */